// components/calendar.js
import api from '../../utils/api.js';
import network from '../../utils/network.js';
import formatDate from '../../utils/format_date';

Component({

    /**
     * 组件的属性列表
     */
    properties: {
        visible: {
            type: Boolean,
            value: false,
        },
        value: {
            type: Object,
            value: {
                timerAmount: {
                    type: Array,
                    value: []
                },
                list: {
                    type: Array,
                    value: []
                },
                siteId: {
                    type: Number,
                    value: 0
                }
            },
            observer(value) {
                const timerAmount = value.timerAmount;
                if (!timerAmount) {
                    return;
                }
                const date = new Date();
                const nowYear = date.getFullYear();
                const nowMonth = date.getMonth() + 1;
                this.setData({
                    year: nowYear,
                    month: nowMonth
                })
                let dateList = [{
                    year: nowYear,
                    month: nowMonth,
                    dates: this.getDates(nowYear, nowMonth, timerAmount)
                }];
                if (nowMonth == 11) {
                    dateList.push({
                        year: nowYear,
                        month: nowMonth + 1,
                        dates: this.getDates(nowYear, nowMonth + 1, timerAmount)
                    })

                    dateList.push({
                        year: nowYear + 1,
                        month: 1,
                        dates: this.getDates(nowYear, 1, timerAmount)
                    })
                } else if (nowMonth == 12) {
                    dateList.push({
                        year: nowYear + 1,
                        month: 1,
                        dates: this.getDates(nowYear + 1, 1, timerAmount)
                    })

                    dateList.push({
                        year: nowYear + 1,
                        month: 2,
                        dates: this.getDates(nowYear, 2, timerAmount)
                    })
                } else {
                    dateList.push({
                        year: nowYear,
                        month: nowMonth + 1,
                        dates: this.getDates(nowYear, nowMonth + 1, timerAmount)
                    })

                    dateList.push({
                        year: nowYear,
                        month: nowMonth + 2,
                        dates: this.getDates(nowYear, nowMonth + 2, timerAmount)
                    })
                }

                let list = value.list;
                if (list.length > 0) {
                    //year + '年' + month + '月' + day
                    for (let i = 0; i < dateList.length; i++) {
                        let monthDatas = dateList[i];
                        for (let j = 0; j < monthDatas.dates.length; j++) {
                            let dayDatas = monthDatas.dates[j];
                            for (let k = 0; k < dayDatas.length; k++) {
                                if (dayDatas[k] != 0) {
                                    let timerDatas = dayDatas[k].timer;
                                    for (let l = 0; l < timerDatas.length; l++) {
                                        let month = monthDatas.month;
                                        let day = dayDatas[k].day;
                                        if (month < 10) {
                                            month = '0' + month;
                                        }
                                        if (day < 10) {
                                            day = '0' + day;
                                        }
                                        let id = monthDatas.year + '' + month + '' + day + '' + timerDatas[l].id;
                                        for (let m = 0; m < list.length; m++) {
                                            if (id == list[m].id) {
                                                dateList[i].dates[j][k].timer[m].selected = true;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                this.setData({
                    dateList
                })
                this.getBookingTimeAppMonth(value.siteId, dateList);
            },
        },
    },

    /**
     * 组件的初始数据
     */
    data: {
        dates: [],
        week: ['日', '一', '二', '三', '四', '五', '六'],
        selectedTimerIndex: 0,
        dateList: [],
        current: 0
    },

    lifetimes: {
        created: function () {},
        attached: function () {},
        ready: function () {},
        detached: function () {},
    },

    pageLifetimes: {
        // 组件所在页面的生命周期函数
        show: function () {
            this.list = [];
        },
        hide: function () {},
    },

    /**
     * 组件的方法列表
     */
    methods: {
        getDates(year, month, timer) {
            var dates = [];
            var startDate = new Date(year, month - 1, 1);
            var endDate = new Date(year, month, 0);
            var dayOfWeek = startDate.getDay();
            for (var i = 0; i < dayOfWeek; i++) {
                dates.push(0);
            }

            const nowYear = this.data.year;
            const nowMonth = this.data.month;
            const date = new Date();
            const nowDay = date.getDate();

            if (month < 10) {
                month = '0' + month;
            }

            for (var i = startDate.getDate(); i <= endDate.getDate(); i++) {
                let timerAmount = [{
                    id: 1,
                    mode: '上午',
                    time: '09:00-12:00',
                    amount: timer[0],
                    selected: false
                }, {
                    id: 2,
                    mode: '下午',
                    time: '14:00-17:00',
                    amount: timer[1],
                    selected: false
                }, {
                    id: 3,
                    mode: '晚间',
                    time: '19:00-22:00',
                    amount: timer[2],
                    selected: false
                }];
                let day = i;
                if (day < 10) {
                    day = '0' + day;
                }
                if (nowYear == year && nowMonth == month) {
                    if (i == nowDay) {
                        dates.push({
                            id: year + '' + month + '' + day,
                            status: 'normal', // disabled, full, normal, selecting, selected
                            day: i,
                            timer: timerAmount,
                            isToday: true,
                            des: '可订'
                        });
                    } else if (i < nowDay) {
                        dates.push({
                            id: year + '' + month + '' + day,
                            status: 'disabled',
                            day: i,
                            timer: timerAmount,
                            isToday: false,
                            des: ''
                        });
                    } else {
                        dates.push({
                            id: year + '' + month + '' + day,
                            status: 'normal',
                            day: i,
                            timer: timerAmount,
                            isToday: false,
                            des: '可订'
                        });
                    }
                } else {
                    dates.push({
                        id: year + '' + month + '' + day,
                        status: 'normal',
                        day: i,
                        timer: timerAmount,
                        isToday: false,
                        des: '可订'
                    });
                }
            }
            const arr = [];
            for (let i = 0; i < dates.length; i += 7) {
                arr.push(dates.slice(i, i + 7));
            }
            return arr;
        },

        getBookingTimeAppMonth(siteId, dateList) {
            let monthDate = dateList[this.data.current];
            network.getJson(api.timerMonth, {
                siteId,
                year: monthDate.year,
                month: monthDate.month
            }).then(res => {
                let data = res.data;
                for (let i = 0; i < dateList[this.data.current].dates.length; i++) {
                    for (let k = 0; k < dateList[this.data.current].dates[i].length; k++) {
                        for (let j = 0; j < data.length; j++) {
                            if (dateList[this.data.current].dates[i][k].day == data[j].date) {
                                for (let l = 0; l < dateList[this.data.current].dates[i][k].timer.length; l++) {
                                    if (data[j].timer.indexOf(dateList[this.data.current].dates[i][k].timer[l].id) != -1) {
                                        dateList[this.data.current].dates[i][k].timer[l].disabled = 1;
                                    }
                                }
                                if (dateList[this.data.current].dates[i][k].status != 'disabled') {
                                    if (data[j].timer.length == 5) {
                                        dateList[this.data.current].dates[i][k].status = 'full';
                                        dateList[this.data.current].dates[i][k].des = '已满';
                                    } else {
                                        dateList[this.data.current].dates[i][k].status = 'normal';
                                        dateList[this.data.current].dates[i][k].des = '可订';
                                    }
                                }
                            }
                        }
                    }
                }
                this.setData({
                    [`dateList[${this.data.current}]`]: this.data.dateList[this.data.current]
                })
            })
        },

        leftTap() {
            if (this.data.current == 0) {
                return;
            }
            this.setData({
                current: this.data.current - 1
            })
        },

        rightTap() {
            if (this.data.current == 5) {
                return;
            }
            this.setData({
                current: this.data.current + 1
            })
        },

        onVisibleChange(e) {
            this.setData({
                visible: e.detail.visible,
            });
        },

        handleSelect(e) {
            const index = e.currentTarget.dataset.index;
            const idx = e.currentTarget.dataset.idx;
            if (this.data.dateList[this.data.current].dates[index][idx].status === 'disabled') {
                return;
            }
            if (this.data.dateList[this.data.current].dates[index][idx].status === 'full') {
                return;
            }
            this.status = this.data.dateList[this.data.current].dates[index][idx].status;
            if (this.status === 'selected') {
                this.setData({
                    [`dateList[${this.data.current}].dates[${index}][${idx}].status`]: 'normal'
                })
                return;
            }
            for (let i = 0; i < this.data.dateList[this.data.current].dates.length; i++) {
                for (let j = 0; j < this.data.dateList[this.data.current].dates[i].length; j++) {
                    if (this.data.dateList[this.data.current].dates[i][j].status === 'selected') {
                        this.data.dateList[this.data.current].dates[i][j].status = 'normal';
                    }
                }
            }
            this.data.dateList[this.data.current].dates[index][idx].status = 'selected';
            this.setData({
                selectedTimerIndex: idx,
                [`dateList[${this.data.current}].dates`]: this.data.dateList[this.data.current].dates
            })
        },

        timerTap(e) {
            const index = e.currentTarget.dataset.index;
            const idx = e.currentTarget.dataset.idx;
            const i = e.currentTarget.dataset.i;

            let day = this.data.dateList[this.data.current].dates[index][idx];
            let timer = day.timer[i];
            let startTime = timer.time.split('-')[1];

            let month = this.data.dateList[this.data.current].month;
            if (month < 10) {
                month = '0' + month
            }

            let d = day.day;
            if (d < 10) {
                d = '0' + d
            }
            let startDate = this.data.dateList[this.data.current].year + "-" + month + "-" + d + " " + startTime + ":00";
            console.log(startDate)
            if (new Date().getTime() >= new Date(startDate).getTime()) {
                wx.showToast({
                    icon: 'none',
                    title: '预订时间已过'
                })
                return;
            }

            this.setData({
                [`dateList[${this.data.current}].dates[${index}][${idx}].timer[${i}].selected`]: !timer.selected
            })

            // let year = this.data.dateList[this.data.current].year;
            // let month = this.data.dateList[this.data.current].month;
            // let day = this.data.dateList[this.data.current].dates[index][idx].day;
            // if (month < 10) {
            //     month = '0' + month;
            // }
            // if (day < 10) {
            //     day = '0' + day;
            // }
            // if (timer.selected) {
            //     let time = timer.time.split('-');
            //     let startTime = time[0] + ':00';
            //     let endTime = time[1] + ':00';

            //     this.list.push({
            //         id: year + '' + month + '' + day + '' + timer.id,
            //         mode: timer.mode,
            //         showTime: year + '年' + month + '月' + day + '日',
            //         startTime: year + '-' + month + '-' + day + ' ' + startTime,
            //         endTime: year + '-' + month + '-' + day + ' ' + endTime,
            //         amount: timer.amount,
            //         current: this.data.current
            //     })
            // } else {
            //     const id = year + '' + month + '' + day + '' + timer.id;
            //     for (let i = 0; i < this.list.length; i++) {
            //         if (this.list[i].id == id) {
            //             this.list.splice(i, 1);
            //         }
            //     }
            // }
        },

        submit() {
            this.setData({
                visible: false
            });
            let list = [];
            for (let i = 0; i < this.data.dateList.length; i++) {
                let monthDatas = this.data.dateList[i];
                for (let j = 0; j < monthDatas.dates.length; j++) {
                    let dayDatas = monthDatas.dates[j];
                    for (let k = 0; k < dayDatas.length; k++) {
                        if (dayDatas[k] != 0) {
                            let timerDatas = dayDatas[k].timer;
                            for (let l = 0; l < timerDatas.length; l++) {
                                let timer = timerDatas[l];
                                if (timer.selected == true) {
                                    let year = monthDatas.year;
                                    let month = monthDatas.month;
                                    let day = dayDatas[k].day;
                                    if (month < 10) {
                                        month = '0' + month;
                                    }
                                    if (day < 10) {
                                        day = '0' + day;
                                    }
                                    let time = timer.time.split('-');
                                    let startTime = time[0] + ':00';
                                    let endTime = time[1] + ':00';

                                    list.push({
                                        id: year + '' + month + '' + day + '' + timer.id,
                                        mode: timer.id,
                                        showTime: year + '年' + month + '月' + day + '日',
                                        startTime: year + '-' + month + '-' + day + ' ' + startTime,
                                        endTime: year + '-' + month + '-' + day + ' ' + endTime,
                                        amount: timer.amount
                                    })
                                }
                            }
                        }
                    }
                }
            }
            this.triggerEvent('submit', list)
        }
    }
})